﻿
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ParameterController : ControllerBase
    {
        public ParameterController()
        {

        }

        /// <summary>
        /// 参数读取
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("GetParam")]
        public ResponseMessage GetParam()
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                List<Address> list = serial.GetParam(501, 17);

                message.errcode = 0;
                message.errmsg = "";
                message.data = list;
                message.total = list.Count;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "参数读取异常";
            }

            return message;
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("SetParam")]
        public ResponseMessage SetParam(List<Address> list)
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                bool result = serial.SetParam(list);
                if (result)
                {
                    message.errcode = 0;
                    message.errmsg = "";
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "设置失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "参数读取异常";
            }

            return message;
        }
    }
}
